using System;
using System.Web;
using AsyncUploaderDemo.Models;
using System.IO;
using System.Web.Hosting;
using System.Drawing;
using System.Drawing.Drawing2D;
using WarOfDev.EF;
using WarOfDev.Web.Controllers;

namespace AsyncUploaderDemo.Models
{
    internal class DiskFileStore : IFileStore
    {
        private string _uploadsFolder = HostingEnvironment.MapPath("~/Content/Files/");
        public static char DirSeparator = System.IO.Path.DirectorySeparatorChar;
        public static string FilesPath = HttpContext.Current.Server.MapPath(string.Format("Content{0}Files{1}", DirSeparator, DirSeparator));
        public static string fileName = "";
        private BaseController developer = new BaseController();

        public Guid SaveUploadedFile(HttpPostedFileBase fileBase)
        {
            var identifier = Guid.NewGuid();
            //fileName = identifier + suffix(fileBase.FileName);
            fileName = fileBase.FileName;
            fileBase.SaveAs(_uploadsFolder + fileName);
            ResizeImage(fileBase, 100, 50);
            return identifier;
        }

        private string GetDiskLocation(Guid identifier)
        {
            return Path.Combine(_uploadsFolder, identifier.ToString());
        }

        private string suffix(string fileName)
        {
            string[] names = fileName.Split('.');

            return "."+names[1].ToString();
        }



        public void ResizeImage(HttpPostedFileBase file, int width, int height)
        {

            string thumbnailDirectory =
            String.Format(@"{0}{1}", FilesPath, this.developer.CurrentUser.TrueName);
            // Check if the directory we are saving to exists
            if (!Directory.Exists(thumbnailDirectory))
            {
                // If it doesn't exist, create the directory
                Directory.CreateDirectory(thumbnailDirectory);
            }
            // Final path we will save our thumbnail
            string imagePath =
            String.Format(@"{0}{1}{2}", thumbnailDirectory,
            DirSeparator, DiskFileStore.fileName);
            // Create a stream to save the file to when we're
            // done resizing
            FileStream stream = new FileStream(Path.GetFullPath(
            imagePath), FileMode.OpenOrCreate);
            // Convert our uploaded file to an image
            Image OrigImage = Image.FromStream(file.InputStream);
            // Create a new bitmap with the size of our
            // thumbnail
            Bitmap TempBitmap = new Bitmap(width, height);
            // Create a new image that contains quality
            // information
            Graphics NewImage = Graphics.FromImage(TempBitmap);
            NewImage.CompositingQuality =
            CompositingQuality.HighQuality;
            NewImage.SmoothingMode =
            SmoothingMode.HighQuality;
            NewImage.InterpolationMode =
            InterpolationMode.HighQualityBicubic;
            // Create a rectangle and draw the image
            Rectangle imageRectangle = new Rectangle(0, 0,
            width, height);
            NewImage.DrawImage(OrigImage, imageRectangle);
            // Save the final file
            TempBitmap.Save(stream, OrigImage.RawFormat);
            // Clean up the resources
            NewImage.Dispose();
            TempBitmap.Dispose();
            OrigImage.Dispose();
            stream.Close();
            stream.Dispose();
        }


    }
}